package com.zmn.plat.admin.controller.product.guarantee;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.model.entity.product.guarantee.ProductGuarantee;

/**
 * 产品保障管理
 *
 * @author linfeng
 *
 * @since 2020-03-17 10:16:02
 */
@RestController
@RequestMapping("/showProduct/guarantee/")
public class ShowProductGuaranteeModifyController extends ShowProductGuaranteeBaseController {

    /**
     * 新增修改
     * 
     * @author linfeng
     * @param productGuarantee
     * @since 2020-03-17 10:16:02
     */
    @RequestMapping(value = "addEdit")
    public ResponseDTO addEdit(@RequestBody ProductGuarantee productGuarantee) {
        try {
            ProductGuarantee dbResult = productGuaranteeService.findByProductId(productGuarantee.getProductId());
            if (dbResult != null) {
                productGuarantee.setProductGuaranteeId(dbResult.getProductGuaranteeId());
            }

            if (productGuarantee.getProductGuaranteeId() == null) {
                productGuarantee.setCreater(getMcStaff().getRealName());
                productGuarantee.setCreateTime(DateUtil.getNow());
                productGuarantee.setUpdater(getMcStaff().getRealName());
                productGuarantee.setUpdateTime(DateUtil.getNow());
                Integer result = productGuaranteeService.insert(productGuarantee);
                return ResponseDTO.success(result, "新增产品保障成功！");
            } else {
                productGuarantee.setUpdater(getMcStaff().getRealName());
                productGuarantee.setUpdateTime(DateUtil.getNow());
                Integer result = productGuaranteeService.updateByKey(productGuarantee);
                return ResponseDTO.success(result, "修改产品保障成功！");
            }
        } catch (Exception ex) {
            logger.error(String.format("[zmn]新增/修改产品保障失败,productGuarantee:[%s],ex:[%s]", ex.getMessage(), ex));
            return ResponseDTO.fail("操作失败！");
        }
    }
}
